package com.xc.hotelmgr.webapi.modules.dt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;
import com.xc.hotelmgr.webapi.modules.dt.dao.DtOrderChannelsDao;
import com.xc.hotelmgr.webapi.modules.dt.entity.DtOrderChannelsEntity;
import com.xc.hotelmgr.webapi.modules.dt.entity.StoreEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtOrderChannelsService;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.modules.ord.service.OrderFeignService;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 采购渠道
 */
@RestController
@RequestMapping("dt/channel")
@RequiredArgsConstructor
public class DtOrderChannelsController extends AbstractController {

    private final DtOrderChannelsService dtOrderChannelsService;

    private final DtOrderChannelsDao dtOrderChannelsDao;

    private final OrderFeignService orderFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("generator:dtorderchannels:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dtOrderChannelsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("generator:dtorderchannels:info")
    public R info(@PathVariable("id") Long id){
		DtOrderChannelsEntity dtOrderChannels = dtOrderChannelsService.getById(id);
        return R.ok().put("dtOrderChannels", dtOrderChannels);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("generator:dtorderchannels:save")
    public R save(@RequestBody DtOrderChannelsEntity dtOrderChannels){
        Integer storeId = dtOrderChannels.getStoreId();
        Integer chanel = dtOrderChannels.getChanel();
        DtOrderChannelsEntity dtOrderChannelsEntity = dtOrderChannelsDao.getByStoreIdAndChannel(storeId, chanel);
        if(dtOrderChannelsEntity!=null) {
            return R.error("预定渠道已存在");
        }
		dtOrderChannelsService.save(dtOrderChannels);
        orderFeignService.refesh();
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("generator:dtorderchannels:update")
    public R update(@RequestBody DtOrderChannelsEntity dtOrderChannels){
		dtOrderChannelsService.updateById(dtOrderChannels);
        orderFeignService.refesh();
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("generator:dtorderchannels:delete")
    public R delete(@RequestBody Long[] ids){
		dtOrderChannelsService.removeByIds(Arrays.asList(ids));
        orderFeignService.refesh();
        return R.ok();
    }

}
